package org.zjsru.order.web;

import org.zjsru.order.feign.ProductFeignClient;
import org.zjsru.order.feign.UserFeignClient;
import org.zjsru.order.pojo.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    private static final Logger log = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private UserFeignClient userFeignClient;
    
    @Autowired
    private ProductFeignClient productFeignClient;

    @GetMapping("/test")
    @ResponseBody
    public String test() {
        log.info("收到测试请求");
        return "订单服务正常运行中！";
    }

    @PostMapping("/create")
    @ResponseBody
    public String createOrder(
            @RequestParam("userId") String userId,
            @RequestParam("productId") String productId,
            @RequestParam("count") int count
    ) {
        log.info("创建订单：userId={}, productId={}, count={}", userId, productId, count);
        try {
            // 1. 调用用户服务验证用户
            String userInfo = userFeignClient.getUserInfo(userId);
            log.info("用户信息: {}", userInfo);
            
            // 2. 调用商品服务扣减库存
            String productResult = productFeignClient.deductStock(productId, count);
            log.info("商品结果: {}", productResult);
            
            return "订单创建成功：" + userInfo + " | " + productResult;
        } catch (Exception e) {
            log.error("创建订单失败", e);
            return "创建订单失败：" + e.getMessage();
        }
    }
}
